package com.library.management.service.notice.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.library.management.domain.notice.Notice;
import com.library.management.mapper.notice.NoticeMapper;
import com.library.management.service.notice.NoticeService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {
    
    @Override
    public List<Notice> getActiveNotices(Integer limit) {
        LambdaQueryWrapper<Notice> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Notice::getStatus, 1) // 只查询启用的公告
               .orderByDesc(Notice::getPublishTime) // 按发布时间倒序
               .last("LIMIT " + limit); // 限制数量
        return this.list(wrapper);
    }
    
    @Override
    public boolean publishNotice(Notice notice) {
        // 设置发布时间
        notice.setPublishTime(LocalDateTime.now());
        // 设置状态为启用
        notice.setStatus(1);
        return this.save(notice);
    }
    
    @Override
    public boolean deleteNotice(Long id) {
        return this.removeById(id);
    }
} 